package com.sctvcloud.weiyuan.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.jiongbull.jlog.JLog;
import com.luck.picture.lib.config.PictureConfig;
import com.ruihang.generalibrary.ui.util.OnItemInternalClick;
import com.ruihang.generalibrary.utils.ListUtils;
import com.sctvcloud.weiyuan.R;
import com.sctvcloud.weiyuan.beans.GalleryBean;
import com.sctvcloud.weiyuan.http.AbsListNetCallback;
import com.sctvcloud.weiyuan.http.NetUtils;
import com.sctvcloud.weiyuan.ui.activities.GalleryActivity;
import com.sctvcloud.weiyuan.ui.adapter.GalleryAdapter;
import com.sctvcloud.weiyuan.utils.ListTypeUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 图片栏目
 */
public class GalleryFragment extends ColumnBaseFragment implements CanRefreshLayout.OnRefreshListener
        , CanRefreshLayout.OnLoadMoreListener {

    @BindView(R.id.common_refresh)
    protected CanRefreshLayout refreshLayout;
    @BindView(R.id.can_content_view)
    protected RecyclerView recyclerView;
    private GalleryAdapter adapter;
    private int mPageNo = 0;
    private int mPageAll = 0;

    private boolean mIsLoading = false;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_gallery, container, false);
        ButterKnife.bind(this, view);
        return view;
    }


    @Override
    public void onLoadMore() {
        getChannelList();
    }

    @Override
    public void onRefresh() {
        if (mIsLoading) {
            return;
        }
        mPageNo = 0;
        mIsLoading = true;
        getChannelList();

    }

    @Override
    protected void initViews(View v, @Nullable Bundle savedInstanceState) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadMoreListener(this);
        refreshLayout.setLoadMoreEnabled(true);
        refreshLayout.autoRefresh();

    }

    private synchronized void onNetFinish() {
        mIsLoading = false;
        refreshLayout.refreshComplete();
        refreshLayout.loadMoreComplete();
        refreshLayout.setLoadMoreEnabled(mPageNo >= mPageAll ? false : true);
    }


    /**
     * 获取精选模块 多个类型的显示数据。
     */
    public void getChannelList() {
        if (TextUtils.isEmpty(channelUrl)) {
            toast("数据错误");
            JLog.e("int " + this.getClass().getSimpleName() + " url wrong ::" + channelUrl);
            refreshLayout.refreshComplete();
            return;
        }

        NetUtils.getNetAdapter().getGalleryArray(getOwnerName(), channelUrl,mPageNo ,new AbsListNetCallback<GalleryBean>(ListTypeUtils.FocusColums()) {
            @Override
            public void onSuc(List<GalleryBean> focusColumns) {
                if (!ListUtils.isListValued(focusColumns)) {
                    onError(null, null);
                    return;
                }

                if (mPageAll == 0) {
                    mPageAll = getPageResponse().getData().getPageAll();
                }
                initData(focusColumns);

            }

            @Override
            public void onError(Throwable ex, String errorMsg) {
                refreshLayout.refreshComplete();
            }

            @Override
            public void onEnd() {
                super.onEnd();
                onNetFinish();
            }
        });
    }


    private void initData(List<GalleryBean> focusColumns) {
        if (adapter == null) {
            adapter = new GalleryAdapter(getContext(), focusColumns).setItemInternalClick(newsItemClick);
            recyclerView.setAdapter(adapter);
        }

        if (mPageNo == 0) {
            adapter.setData(focusColumns);
        } else {
            adapter.addDatas(focusColumns);
        }
        mPageNo++;
    }

    OnItemInternalClick newsItemClick = new OnItemInternalClick() {
        @Override
        public void onItemInternalClick(View itemView, View view, int position) {
                GalleryBean newsItem = (GalleryBean) itemView.getTag(R.id.adapter_item_data_key);
                Intent intent = new Intent(getActivity(), GalleryActivity.class);
                intent.putExtra(GalleryActivity.EXTRA_URL, newsItem.getNewsUrl());
                intent.putExtra(PictureConfig.EXTRA_POSITION, position);
                getActivity().startActivity(intent);

        }
    };

}
